package com.example.wxchatdemo.utils;


import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


import com.example.wxchatdemo.pojo.WeChat_msgs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyThread extends Thread implements Callable
{
    private String address="100.2.193.182";
    private String my_id,youid;
    private List<WeChat_msgs> lists;
    private int x;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public MyThread(String my_id, String youid, List<WeChat_msgs> lists, String value) {
        this.my_id = my_id;
        this.youid = youid;
        this.lists = lists;
        this.value = value;
    }

    public MyThread() {
    }

    public MyThread(@Nullable Runnable target) {
        super(target);
    }

    public MyThread(@Nullable ThreadGroup group, @Nullable Runnable target) {
        super(group, target);
    }

    public MyThread(@NonNull String name) {
        super(name);
    }

    public MyThread(@Nullable ThreadGroup group, @NonNull String name) {
        super(group, name);
    }

    public MyThread(@Nullable Runnable target, @NonNull String name) {
        super(target, name);
    }

    public MyThread(@Nullable ThreadGroup group, @Nullable Runnable target, @NonNull String name) {
        super(group, target, name);
    }

    public MyThread(@Nullable ThreadGroup group, @Nullable Runnable target, @NonNull String name, long stackSize) {
        super(group, target, name, stackSize);
    }

    public String getMy_id() {
        return my_id;
    }

    public void setMy_id(String my_id) {
        this.my_id = my_id;
    }

    public String getYouid() {
        return youid;
    }

    public void setYouid(String youid) {
        this.youid = youid;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    private String value;
    @Override
    public void run() {
        //
        List listss = new ArrayList<WeChat_msgs>();

        String result = null;
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://" + address + ":8085/GetMsgs?youid=" + youid + "&myid=" + my_id).build();
        //数据是：//phone="+phone.getText()+"&name="+username.getText()+"&password="+password.getText())
        try (Response response = client.newCall(request).execute()) {
            System.out.println("启动MYThread---");
            result = response.body().string();
             System.out.println(result);
            //转化JSON函数
            TransJSON transJSON=new TransJSON();
            List<Map<String, String>> list = transJSON.ttransJSON(result);

            System.out.println("获得MyThread结果集数量"+list.size());
            //循环取出json
            //  while (inde<list.size()){
            for (int i=0;i<list.size();i++) {
                String id = list.get(i).get("id");
                String myid = list.get(i).get("myid");
                String youid = list.get(i).get("youid");
                String content = list.get(i).get("content");
                String times = list.get(i).get("times");
                String status = list.get(i).get("status");
                //System.out.println("GetJSon方法结果集"+"id="+id+"---myid--="+myid+"--youid="+youid+"--time="+times+"==="+content);


                //创建实体类
                WeChat_msgs weChat_msgs = new WeChat_msgs();
                weChat_msgs.setId(id);
                weChat_msgs.setMyid(myid);
                weChat_msgs.setContent(content);
                weChat_msgs.setTimes(times);
                weChat_msgs.setYouid(youid);
                weChat_msgs.setStatus(status);
                // System.out.println("在Myhendl中出现了status的数量+"+status);
                listss.add(weChat_msgs);
        }

        setLists(listss);
        setX(1);

           // System.out.println("查询的数据是==="+listss.size());
        } catch (IOException e) {

            System.out.println("=====MyThread+++线程出错了");
            setLists(null);
            setX(1);

        }

  }//run


    @Override
    public List<WeChat_msgs> call() throws Exception {
        while (true){
            if (getX()==1){
                return lists;
            }

        }
    }




}
